<?php


namespace apps\marriage\app\mp\controller;


use apps\marriage\core\service\CourseOrderService;
use apps\marriage\core\service\CourseService;
use think\Exception;
use think\Request;

class CourseOrderController extends BaseMarriageMpController
{
    /**
     * @var CourseOrderService
     */
    private $courseOrderService;

    /**
     * @var CourseService
     */
    private $courseService;

    protected function init()
    {
        parent::init();
        $this->courseOrderService = service('CourseOrder', SERVICE_NAMESPACE);
        $this->courseService = service('Course', SERVICE_NAMESPACE);
    }

    public function lists(Request $request)
    {
        $pageSize = $request->param('page_size', 10);
        $params = [
            'user_id' => $this->user['id']
        ];
        $res = $this->courseOrderService->pageListByParams($params, $pageSize);
        if ($res['data']) {
            $dataList = [];
            foreach ($res['data'] as $rs) {
                $course = $this->courseService->getByPk($rs['course_id']);
                $course = $this->courseService->format($course);
                $dataList[] = [
                    'course' => $course,
                    'order' => $this->courseOrderService->format($rs)
                ];
            }
            $res['data'] = $dataList;
        }
        return $this->success($res);
    }

    public function create(Request $request)
    {
        $fromuid = $request->param('fromuid', 0, 'intval');
        $courseId = $request->param('course_id', 0, 'intval');
        $moneyType = $request->param('money_type', 'money');

        if($fromuid == 0) {
            $fromuid = $this->user['fromuid'];
        } else if($fromuid > 0 && $fromuid == $this->user['id']) {
            $fromuid = 0;
        }

        $course = $this->courseService->getByPk($courseId);
        if(empty($course) || $course['is_sale'] == 0) {
            throw new Exception('课程已下线');
        }

        $order = [
            'user_id' => $this->user['id'],
            'fromuid' => $fromuid,
            'course_id' => $courseId,
            'money' => $course['charge'],
            'discount_money' => 0,
            'pay_money' => $course['charge'],
        ];
        $orderSn = $this->courseOrderService->createOrder($order, $moneyType);
        return $this->success(['order_sn' => $orderSn]);
    }

    public function info(Request $request)
    {
        $sn = $request->param('sn');
        $order = $this->courseOrderService->getBySn($sn);
        if(empty($order) || $order['user_id'] != $this->user['id']) {
            throw new Exception('订单不存在');
        }
        $order = $this->courseOrderService->format($order);
        $course = $this->courseService->getByPk($order['course_id']);
        $course = $this->courseService->format($course);
        return $this->success([
            'course' => $course,
            'order' => $order
        ]);
    }

    public function pay_by_gold_coin(Request $request)
    {
        $sn = $request->param('sn');
        $order = $this->courseOrderService->getBySn($sn);
        if(empty($order) || $order['user_id'] != $this->user['id']) {
            throw new Exception('订单不存在');
        }
        $this->courseOrderService->onPayByGoldCoin($sn);
        return $this->success();
    }

    public function cancel(Request $request)
    {
        $sn = $request->param('sn');
        $order = $this->courseOrderService->getBySn($sn);
        if(empty($order) || $order['user_id'] != $this->user['id']) {
            throw new Exception('订单不存在');
        }
        $this->courseOrderService->onCancel($sn);
        return $this->success();
    }
}